package admin

import (
	"blogs/models"
	"blogs/utils"
	beego "github.com/beego/beego/v2/server/web"
)

// Operations about object
type TagController struct {
	beego.Controller
}

// @Title Create
// @Description 添加tag
// @Param	Id		query 	  	string	true		"The Id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 objectId is empty
// @router / [post]
func (o *TagController) Post() {
	title := o.GetString("Title")

	tag := models.Tag{
		Title: title,
	}
	err := utils.DB.Create(&tag).Error
	if err != nil {
		o.Data["json"] = map[string]interface{}{
			"msg":  "创建错误",
			"data": "创建错误",
			"code": false,
		}
		o.ServeJSON()
		return
	}
	o.Data["json"] = map[string]interface{}{
		"msg":  "添加成功",
		"data": tag,
		"code": true,
	}
	o.ServeJSON()

}

// @Title GetAll
// @Description 查询所有标签
// @Success 200 {object}
// @Failure 403 :objectId is empty
// @router / [get]
func (o *TagController) GetAll() {
	tag := []models.Tag{}
	utils.DB.Find(&tag)

	o.Data["json"] = map[string]interface{}{
		"msg":  "查询成功",
		"data": tag,
		"code": true,
	}
	o.ServeJSON()
}

// @Title Delete
// @Description 删除tag
// @Param	Id		query 	  	string	true		"The Id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 objectId is empty
// @router / [delete]
func (o *TagController) Delete() {
	objectId := o.GetString("Id")
	tag := []models.Tag{}
	err := utils.DB.Where("id=?", objectId).Delete(&tag).Error
	if err != nil {
		o.Data["json"] = map[string]interface{}{
			"msg":  "删除失败",
			"data": "删除失败",
			"code": false,
		}
		o.ServeJSON()
		return
	}
	o.Data["json"] = map[string]interface{}{
		"msg":  "删除成功",
		"data": "删除成功",
		"code": true,
	}
	o.ServeJSON()

}
